const hospitalDao = require('../dao/hospital');
const userDao = require('../dao/user');

const config = require('../config').config;

const Response = require('../entity/http/Response');

const hospitalService = {
    async addHospitalAndAdmin(reqId, uid, { hospital, department, departmentID, cityId }, { name, userType, phone, account, password }) {
        let result;
        try {
            let dbResult = await hospitalDao.addHospitalAndAdmin(reqId, hospital, department, departmentID, cityId, name, userType, phone, account, password);
            if (dbResult.insertId) {
                result = new Response().success("创建科室成功");
            } else {
                result = new Response().fail(500, "创建科室失败");
            }
        } catch (err) {
            result = new Response().fail(500, err.message);
        }
        return result;
    },
    async addHospital(reqId, uid, { hospital, department, departmentID, cityId }, { id }) {
        let result;
        try {
            let dbResult = await hospitalDao.addHospital(reqId, hospital, department, departmentID, cityId, id);
            if (dbResult.insertId) {
                result = new Response().success("创建科室成功");
            } else {
                result = new Response().fail(500, "创建科室失败");
            }
        } catch (err) {
            result = new Response().fail(500, "创建科室失败");
        }
        return result;
    },
    async delHospital(reqId, uid, { id }) {
        let result;
        try {
            let dbResult = await hospitalDao.delHospital(reqId, id);
            if (dbResult.changedRows) {
                result = new Response().success("删除科室成功");
            } else {
                result = new Response().fail(500, "删除科室失败");
            }
        } catch (err) {
            result = new Response().fail(500, "删除科室失败");
        }
        return result;
    },
    async changeHospital(reqId, uid, { id: departmentId }, { hospital, department, cityId }, { id: userid }) {
        let result;
        try {
            let dbResult = await hospitalDao.changeHospital(reqId, departmentId, hospital, department, cityId, userid);
            if (dbResult.changedRows) {
                result = new Response().success("修改科室信息成功");
            } else {
                result = new Response().fail(500, "修改科室信息时无改动");
            }
        } catch (err) {
            result = new Response().fail(500, "修改科室信息失败");
        }
        return result;
    },
    async getHospital(reqId, uid, { id }) {
        let result;
        try {
            let dbResult = await hospitalDao.getHospital(reqId, id);
            if (dbResult.length) {
                result = new Response().success(dbResult[0]);
            } else {
                result = new Response().fail(500, "该科室不存在");
            }
        } catch (err) {
            result = new Response().fail(500, "获取科室信息失败");
        }
        return result;
    },
    async getAllHospital(reqId, uid) {
        let result;
        try {
            // 查看当前用户类型 - 用于细分权限
            let daoFunc;
            let selfUserType;
            let queryResult = await userDao.getUserType(reqId, uid);
            if (queryResult.length) {
                selfUserType = queryResult[0].userType;
            } else {
                return new Response().fail(400, "疑似攻击 已记录到系统日志中");
            }
            if (selfUserType == 'department-staff') {
                daoFunc = hospitalDao.getAllHospital_usePower;
            } else {
                daoFunc = hospitalDao.getAllHospital;
            }
            // 获取所有科室
            let dbResult = await daoFunc(reqId, uid);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取所有科室失败");
        }
        return result;
    },
    async getAllDevice(reqId, uid, id) {
        let result;
        try {
            let dbResult = await hospitalDao.getAllDevice(reqId, id);
            result = new Response().success(dbResult);
        } catch (err) {
            result = new Response().fail(500, "获取科室下所有设备失败");
        }
        return result;
    }
};

module.exports = hospitalService;